/*
 * 0133. Longest Word
 * 给一个词典，找出其中所有最长的单词。
 * https://www.lintcode.com/problem/longest-word/description
 * 
 * 样例
 * 在词典
 * {
 * "dog",
 * "google",
 * "facebook",
 * "internationalization",
 * "blabla"
 * }
 * 中, 最长的单词集合为 ["internationalization"]
 * 
 * 在词典
 * {
 * "like",
 * "love",
 * "hate",
 * "yes"
 * }
 * 中，最长的单词集合为 ["like", "love", "hate"]
 * 
 * 挑战
 * 遍历两次的办法很容易想到，如果只遍历一次你有没有什么好办法？
 * 
 * 2018.07.13 @jeyming
 */
package longest_word_0133;

import java.util.ArrayList;
import java.util.List;

public class Longest_word_0133 {
    /*
     * @param dictionary: an array of strings
     * @return: an arraylist of strings
     */
    public List<String> longestWords(String[] dictionary) {
        // write your code here
    	List<String> list = new ArrayList<String>();
    	if(dictionary.length >= 1) {
    		int max = dictionary[0].length();
    		list.add(dictionary[0]);
    		for(int i = 1; i < dictionary.length; ++i) {
        		if(max < dictionary[i].length()) {
        			max = dictionary[i].length();
        			list.clear();
        			list.add(dictionary[i]);
        		} else if(max == dictionary[i].length()) {
        			list.add(dictionary[i]);
        		}
        	}
    	}
    	return list;
    }

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
